package com.miskatmobile.bb.linkedin;

import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.ui.UiApplication;

public class LinkedinAPI {
	private final static String API_ROOT_SSL	= "https://api.linkedin.com";
	private static final String API_ROOT		= "http://api.linkedin.com/v1";
	private static final String REQUEST_TOKEN = "/uas/oauth/requestToken";
	private static final String ACCESS_TOKEN	= "/uas/oauth/accessToken";
	private static final String PEOPLE			= "/people/~";
	private RestClient rest;
	private String verifiercode;
	
	public LinkedinAPI(){
		rest = new RestClient();
	}
	public String requestToken(){
		String result = "";
		String url = API_ROOT_SSL + REQUEST_TOKEN;
		rest.setUrl(url);
		//rest.addParam("oauth_callback", "http://google.com");
		rest.execute(RestClient.POST);
		result = rest.getResponse();
		final String token = result.substring(result.indexOf((OAuthLibrary.OAUTH_TOKEN+"="))+(OAuthLibrary.OAUTH_TOKEN+"=").length(), result.indexOf('&'));
        int idxSecret = result.indexOf((OAuthLibrary.OAUTH_TOKEN_SECRET+"="))+(OAuthLibrary.OAUTH_TOKEN_SECRET+"=").length();
		final String tokenSecret = result.substring(idxSecret, result.indexOf('&', idxSecret));
        String authorizeUrl = result.substring(result.indexOf("xoauth_request_auth_url=")+"xoauth_request_auth_rul=".length(),result.lastIndexOf('&'));
        rest.setTokenWithSecret(token, tokenSecret);
        final String urlnya = StringUtil.replaceAll(StringUtil.replaceAll(authorizeUrl, "%3A", ":", false), "%2F", "/", false);
        //UiApplication.getUiApplication().invokeLater(new Runnable(){
        	//public void run(){
        		Browser.getDefaultSession().displayPage(urlnya+"?"+OAuthLibrary.OAUTH_TOKEN+"="+token+"&"+OAuthLibrary.OAUTH_TOKEN_SECRET+"="+tokenSecret);
        	//}
        //});
        
		return "token= " + token + "\nsecret= "+ tokenSecret+ "\nurl = "+authorizeUrl;
	}
	
	public String accessToken(String verifiercode){
		String url = API_ROOT_SSL + ACCESS_TOKEN;
		this.verifiercode = verifiercode;
		rest.setUrl(url);
		rest.addParam("oauth_verifier", verifiercode);
		rest.execute(RestClient.POST);
		String response = rest.getResponse();
		String token = response.substring(response.indexOf((OAuthLibrary.OAUTH_TOKEN+"="))+(OAuthLibrary.OAUTH_TOKEN+"=").length(), response.indexOf('&'));
        int idxSecret = response.indexOf((OAuthLibrary.OAUTH_TOKEN_SECRET+"="))+(OAuthLibrary.OAUTH_TOKEN_SECRET+"=").length();
		String tokenSecret  = response.substring(idxSecret, response.indexOf('&', idxSecret)/*, response.indexOf("&user")*/);
		rest.setTokenWithSecret(token, tokenSecret);
        return response + "\ntoken="+token + "\nsecret: "+tokenSecret +"\n";
	}
	
	public String getPeople(){
		String url = API_ROOT + PEOPLE;
		rest.setUrl(url);
		
		//rest.addParam("oauth_verifier", verifiercode);
		rest.addParam("format", "json");
		rest.execute(RestClient.GET);
		
		String response = rest.getResponse();
		
		return response;
	}
	
	//http://api.linkedin.com/v1/people/~/person-activities
	public String shareActivities(String content){
		String url = "http://api.linkedin.com/v1/people/~/shares";//"http://api.linkedin.com/v1/people/~/person-activities";
		rest.setUrl(url);
		rest.addParam("content", URLUTF8Encoder.encode(content));
		rest.addParam("format", "json");
		rest.execute(RestClient.POST);
		
		return rest.getResponse();
	}
}
